package com.jf.cloud.platform.controller.app;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSONObject;
import com.jf.cloud.common.cache.util.RedisUtil;
import com.jf.cloud.common.constant.ConfigNameConstant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.i18n.LanguageEnum;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.DateUtils;
import com.jf.cloud.platform.dto.InfoCateDTO;
import com.jf.cloud.platform.dto.InformationDTO;
import com.jf.cloud.platform.model.InfoCate;
import com.jf.cloud.platform.model.Information;
import com.jf.cloud.platform.service.InfoCateService;
import com.jf.cloud.platform.service.InformationService;
import com.jf.cloud.platform.service.SysConfigService;
import com.jf.cloud.platform.vo.InfoCateVO;
import com.jf.cloud.platform.vo.InformationVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author lth
 * @Date 2021/6/1 13:59
 */
@RestController("appSysConfigController")
@RequestMapping("/ua/app/sys_config")
@Tag(name = "app-系统配置信息")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private InformationService informationService;

    @Autowired
    private InfoCateService infoCateService;


    @GetMapping("/service_protocol")
    @Operation(summary = "获取服务条款", description = "获取服务条款")
    public ServerResponseEntity<String> getServiceProtocol() {
        Integer dbLang = I18nMessage.getLang();
        if (Objects.equals(LanguageEnum.LANGUAGE_ZH_CN.getLang(), dbLang)) {
            return ServerResponseEntity.success(sysConfigService.getValue(ConfigNameConstant.SERVICE_PROTOCOL_CN));
        } else {
            return ServerResponseEntity.success(sysConfigService.getValue(ConfigNameConstant.SERVICE_PROTOCOL_EN));
        }
    }

    @GetMapping("/privacy_protocol")
    @Operation(summary = "获取隐私策略", description = "获取隐私策略")
    public ServerResponseEntity<String> getPrivacyProtocol() {
        Integer dbLang = I18nMessage.getLang();
        if (Objects.equals(LanguageEnum.LANGUAGE_ZH_CN.getLang(), dbLang)) {
            return ServerResponseEntity.success(sysConfigService.getValue(ConfigNameConstant.PRIVACY_PROTOCOL_CN));
        } else {
            return ServerResponseEntity.success(sysConfigService.getValue(ConfigNameConstant.PRIVACY_PROTOCOL_EN));
        }
    }

    @GetMapping("/service_information")
    @Operation(summary = "获取资讯列表", description = "获取信息资讯")
    public ServerResponseEntity<PageVO<InformationVO>> getServiceInformation(@Valid PageDTO pageDTO, @ParameterObject InformationDTO informationDTO) {
        PageVO<InformationVO> informationPage = informationService.pageList(pageDTO, informationDTO);
        return ServerResponseEntity.success(informationPage);
    }

    @GetMapping("/info_cate")
    @Operation(summary = "获取分类列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<InfoCateVO>> page(@Valid PageDTO pageDTO, @ParameterObject InfoCateDTO infoCateDTO) {
        PageVO<InfoCateVO> infoCatePage = infoCateService.page(pageDTO ,infoCateDTO);
        return ServerResponseEntity.success(infoCatePage);
    }
    @GetMapping("/information_by_id")
    @Operation(summary = "获取资讯", description = "根据id获取资讯")
    public ServerResponseEntity<Information> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(informationService.getById(id));
    }

    @GetMapping("/service_weather")
    @Operation(summary = "获取天气", description = "获取天气资讯")
    public ServerResponseEntity<String> getServiceWeather() {

        if (RedisUtil.hasKey("weather_" + DateUtils.dateToStrYmd(new Date()))) {
            return ServerResponseEntity.success(RedisUtil.get("weather_" + DateUtils.dateToStrYmd(new Date())));
        } else {
            String host = "https://jisutqybmf.market.alicloudapi.com";
            String path = "/weather/query";
            String method = "GET";//GET/POST 任意
            String appcode = "3a93a742ff9d4f8792ce73b7f0354bbb";
            Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            //根据API的要求，定义相对应的Content-Type
            headers.put("Content-Type", "application/json; charset=UTF-8");
            Map<String, String> querys = new HashMap<String, String>();
            querys.put("city", "永州");
            HttpRequest request = HttpUtil.createGet(host + path + "?city=永州");
            request.addHeaders(headers);
            request.setMethod(Method.GET);
            HttpResponse response = request.execute();
            JSONObject jsonObject= JSONObject.parseObject(response.body());
            if(!Objects.isNull(jsonObject)){
                if(jsonObject.getInteger("status")==0) {
                    RedisUtil.set("weather_" + DateUtils.dateToStrYmd(new Date()), response.body(), 12 * 60 * 60);
                    return ServerResponseEntity.success(RedisUtil.get("weather_" + DateUtils.dateToStrYmd(new Date())));
                }else{
                    return ServerResponseEntity.fail(ResponseEnum.DATA_ERROR);
                }
            }else {
                return ServerResponseEntity.fail(ResponseEnum.DATA_ERROR);
            }
        }
    }
}
